Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink JS-allocated strings down to the correct size before passing them to Rust #3808

Merged
merged 3 commits into from
Jan 25, 2024

Conversation

Liamolucko
Copy link
Collaborator

Fixes #3801.

I opted to solve it this way rather than just pass the capacity to Rust as well because it means the allocation isn't up to 3x bigger than it needs to be anymore. I also removed a TODO about fixing that.

@@ -1314,6 +1309,7 @@ impl<'a> Context<'a> {
const ret = encodeString(arg, view);
{debug_end}
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add a check for whether len != offset before reallocing again, to avoid spurious allocs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that, but I didn't think it'd be worth it since in the vast majority of cases the string won't take up the maximum amount of space anyway.

I'd be fine to add it if you want but I don't think the difference would be significant either way.

Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a changelog entry as well considering this is an actual user-facing fix?

…hem to Rust

Fixes rustwasm#3801.

I opted to solve it this way rather than just pass the capacity to Rust
as well because it means the allocation isn't up to 3x bigger than it
needs to be anymore. I also removed a TODO about fixing that.
Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UB when copying strings to WASM.
3 participants